﻿@using System.Security.Claims;
@using Newtonsoft.Json;
<style>
    .layui-form input[type=checkbox] {
        display: none;
    }

    .layui-form-label {
        display: block;
        width: 110px;
        text-align: left;
    }

    .toolbar {
        border: 1px solid #ccc;
    }

    .text {
        border: 1px solid #ccc;
        height: 400px;
    }
</style>
@model Blog.Model.Article
<div class="page-content-wrap">
    <form class="layui-form" id="article">
        <div class="layui-tab" style="margin: 0;">
            <ul class="layui-tab-title">
                <li class="layui-this">文章内容</li>
                <li>其他属性</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    @Html.HiddenFor(a => a.Articleid)
                    <input type="hidden" name="Adminid" value="@User.Identities.First(u => u.IsAuthenticated).FindFirst("id").Value" />
                    <div class="layui-form-item">
                        <label class="layui-form-label">文章名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="Title" required lay-verify="required" placeholder="请输入文章名称" autocomplete="off" class="layui-input" value="@Model.Title">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类模块：</label>
                        <div class="layui-input-block">
                            <select name="Parentid" lay-filter="test" required lay-verify="required">
                                <option value="">请选择分类模块</option>
                                @foreach (var item in ViewData["ParentSel"] as List
                                <Blog.Model.Parent>
                                    )
                                    {
                                <option value="@item.Parentid">@item.Pname</option>
                                    }
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类栏目：</label>
                        <div class="layui-input-block">
                            <select name="CategoryId" required lay-verify="required">
                                <option value="">请选择分类栏目</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签：</label>
                        <div class="layui-input-block">
                            @{
                            var check = "";
                            var str = Model.Recommend;
                            if (str.Trim() == "no") { check = "checked"; }
                                <input type="checkbox" name="Recommend" title="推荐" @check>
                            }
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">文章内容：</label>
                        <input type="hidden" name="content" value="@Model.Content" />
                        <div class="layui-input-block">
                            <div id="div1" class="toolbar">
                            </div>
                            <div style="padding: 5px 0; color: #ccc"></div>
                            <div id="div2" class="text">
                                <!--可使用 min-height 实现编辑区域自动增加高度-->
                                <p>请输入内容</p>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="layui-tab-item">
                    <div class="layui-form-item">
                        <label class="layui-form-label">关键字：</label>
                        <div class="layui-input-block">
                            <input type="text" name="Keyword" placeholder="请输入关键字" autocomplete="off" class="layui-input" required lay-verify="required" value="@Model.Keyword">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">评论状态：</label>
                        <div class="layui-input-block">
                            @{
                            var check1 = "";
                            var str1 = Model.CommentStatus;
                            if (str1.Trim() == "no") { check1 = "checked"; }
                                <input type="checkbox" name="CommentStatus" @check1 lay-skin="switch" required lay-verify="required">
                            }
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">摘要：</label>
                        <div class="layui-input-block">
                            <textarea name="Intro" placeholder="请输入摘要" class="layui-textarea">@Model.Intro</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">作者：</label>
                        <div class="layui-input-block">
                            <input type="text" name="Author" required lay-verify="required" placeholder="请输入作者" autocomplete="off" class="layui-input" value="@Model.Author">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">文章来源：</label>
                        <div class="layui-input-block">
                            <input type="text" name="Source" placeholder="请输入文章来源" autocomplete="off" class="layui-input" value="@Model.Source">
                        </div>
                        @Html.HiddenFor(a => a.ReleaseDate)
                        @Html.HiddenFor(a => a.UpdateDate)
                    </div>
                    <div class="layui-form-item" style="padding-left: 10px;">
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
@section script{
    <script src="~/release/wangEditor.min.js"></script>
    <script src="~/admin/js/common.js" type="text/javascript" charset="utf-8"></script>
    <script>
        layui.use(['form', 'jquery', 'laydate', 'layer', 'laypage', 'dialog', 'element', 'upload', 'layedit'], function () {
            var form = layui.form(),
                layer = layui.layer,
                $ = layui.jquery,
                laypage = layui.laypage,
                laydate = layui.laydate,
                layedit = layui.layedit,
                element = layui.element(),
                dialog = layui.dialog;

            //获取当前iframe的name值
            var iframeObj = $(window.frameElement).attr('name');

            form.on('select(test)', function (data) {
                console.log(data.value); //得到被选中的值
                var option = "<option value=''>请选择分类栏目</option>";
                $.get("/Home/CategorySel?no=" + data.value, function (datas) {
                    for (var i = 0; i < datas.length; i++) {
                        option += "<option value='" + datas[i].categoryId + "'>" + datas[i].cname + "</option>";//拼接option中的内容
                    }
                    $("select[name='CategoryId']").html(option);//将option的拼接内容加入select中，注意选择器是select的ID
                    form.render('select');
                });
            });

            //监听提交
            form.on('submit(formDemo)', function (data) {
                var id = $("input[name='Articleid']").val();
                if (id == "0") {
                    $.ajax({
                        type: "post",
                        url: "/Home/addarticle",
                        data: $("#article").serialize(),
                        dataType: 'Json',
                        async: true,
                        success: function (datas) {
                            parent.layer.msg(datas);
                            //AddPut(data.field.Title);
                            socket(data.field.Title);
                        }
                    });
                } else {
                    $.ajax({
                        type: "post",
                        url: "/Home/updatearticle",
                        data: $("#article").serialize(),
                        dataType: 'Json',
                        async: true,
                        success: function (data) {
                            parent.layer.msg(data);
                        }
                    });
                }

                return false;
            });

            form.render();


        });

    </script>
    <script>
         var decodeHtml = HtmlUtil.htmlDecode('@Model.Content');
        var E = window.wangEditor
        var editor = new E('#div1', '#div2')
        var $text1 = $('input[name="content"]')
        editor.customConfig.uploadImgServer = 'http://localhost:65313/api/values/UpfileImage'
        //editor.customConfig.uploadImgShowBase64 = true
        // editor.customConfig.withCredentials = true
        editor.customConfig.zIndex = 1
        editor.customConfig.uploadImgHooks = {
            customInsert: function (insertImg, result, editor) {
                var url = result.data;
                insertImg(url);
            }
        },
            editor.customConfig.onchange = function (html) {
                $text1.val(html)
            }
        editor.create()

        editor.txt.html(decodeHtml)
    </script>
    <script>
        var ws;
        //var scheme = document.location.protocol === "https:" ? "wss" : "ws";
        //var port = document.location.port ? (":" + document.location.port) : "";
        //var str=scheme + "://" + document.location.hostname + port + "?u=admin";
        function socket(text) {
            ws = new WebSocket('ws://localhost:50339/ws?u=admin');
            ws.onopen = function () {
                AddPut(text);
                // ws.send("hello");
            }
            ws.onmessage = function (evt) {
            }
            ws.onerror = function (evt) {
            }
            ws.onclose = function () {
            }
        }
        $('#close').click(function () {
            ws.close();
        });
        function getNow(s) {
            return s < 10 ? '0' + s : s;
        }
        function AddPut(text) {
            var myDate = new Date();
            var year = myDate.getFullYear();        //获取当前年
            var month = myDate.getMonth() + 1;   //获取当前月
            var date = myDate.getDate();            //获取当前日
            var h = myDate.getHours();              //获取当前小时数(0-23)
            var m = myDate.getMinutes();          //获取当前分钟数(0-59)
            var s = myDate.getSeconds();
            var now = year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s);
            if (ws.readyState == WebSocket.OPEN) {
                var data = '{ "Text": "'+text+'", "Username": "", Type: "Put", "MessagDateTime":"'+now+'"}';
                ws.send(data);
            }
        }
    </script>
}